package com.tl.service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import net.sf.json.JSONObject;

import org.json.JSONArray;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.tl.control.LotteryControl;

/**
 * 总服务
 * @author Administrator
 *
 */
public class BusService {

	private LotteryControl lotteryControl;

	public LotteryControl getLotteryControl() {
		return lotteryControl;
	}
	
	 public static void main(String args[]) throws Exception {     
		 parseJson();
//		 Domain  d = new Domain();
//		 d.setId(23);
//		 d.setName("aaaaa");
//		 Domain d2 = (Domain) new BusService().copy(d);
//		 System.out.println(d2.getId()+"...."+d2.getName());
     }
	 
	public Object copy(Object object) throws Exception, NoSuchMethodException{
		Class<? extends Object> clazz = object.getClass();
		
		Object newObject = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
		
		Field[] fields = clazz.getDeclaredFields();
		for(int i=0;i<fields.length;i++){
			Field field = fields[i];
			
			
			String name = field.getName();
			String pre = name.substring(0,1).toUpperCase();
			String getName = "get"+pre+name.substring(1);
			String setName = "set"+pre+name.substring(1);
			Method getMethod = clazz.getMethod(getName, new Class[]{});
			Method setMethod = clazz.getMethod(setName, new Class[]{field.getType()});
			
			Object obj = getMethod.invoke(object, new Object[] {});
			System.out.println("字段名称："+name+" 返回值："+obj);
			
			setMethod.invoke(newObject, new Object[]{obj});
		}
		
		return newObject;
	}
	
	public static void parseJson(){
		String json = "["
			+"{\"id\":1,\"name\":\"demo1\",\"count\":3},"
			+"{\"id\":2,\"name\":\"demo2\",\"count\":4}," 
			+"{\"id\":3,\"name\":\"demo3\",\"count\":5}"
		    + "]";
	
		    net.sf.json.JSONArray array = net.sf.json.JSONArray.fromObject(json);
		    for(int i=0;i<array.size();i++){
		    	JSONObject obj = array.getJSONObject(i);
		    	System.out.println(obj.get("id")+"..."+obj.get("name")+"..."+obj.get("count"));
		    }

	}

}
